//
//  YRStatusTabBar.swift
//  新浪微博
//
//  Created by Viper on 16/6/29.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

class YRStatusTabBar: UIView {
    var retweetButton : UIButton?
    var commentButton : UIButton?
    var unlikeButton : UIButton?
    //供外界赋值
    var status : YRStauts?{
        
        didSet{
            

//            //设置button的title
            
            retweetButton?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            commentButton?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            unlikeButton?.setTitle(status?.attitudesStr, forState: UIControlState.Normal)
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    func getCountString(count : Int,title : String)->String{
        
        if count <= 0 {
            
            return title
            
        }else if count > 0 && count < 10000{
            
            return "\(count)"
        }else{
            
            let countF = CGFloat(count / 1000)/10
            
            var countStr = "\(countF)"
//            containsString 判断字符串是否包含字符
            if countStr.containsString(".0"){
            countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return "\(countStr)万"
        }
  }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        
        backgroundColor = UIColor.clearColor() 
        //添加控件
        retweetButton = addChildButtons("timeline_icon_retweet", title: "转发")
        commentButton = addChildButtons("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButtons("timeline_icon_unlike", title: "赞")
        //分割线
        let line1 = addChildLines()
        
        let line2 = addChildLines()
        
        //添加约束
        retweetButton?.snp_makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton?.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton?.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        line2.snp_makeConstraints { (make) in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
        
    }
    
    
    //MARK: - 创建Button
    private func addChildButtons(imageName:String,title:String)->UIButton{
        
        let button = UIButton()
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
    
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        //添加控件
        addSubview(button)
        
        return button
    }
  
    private func addChildLines()->UIImageView{
        
        let image = UIImageView(imageName: "timeline_card_bottom_line")
        
        //添加
        addSubview(image)
        
        return image
        
    }
    
    
    
}
